const router = require("express").Router();
const db = require("../config/db");
// 时间转换
function formatDate(date, fmt) {
  if (!date) {
    return "";
  }
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(
      RegExp.$1,
      (date.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  }
  let o = {
    "M+": date.getMonth() + 1,
    "d+": date.getDate(),
    "h+": date.getHours(),
    "m+": date.getMinutes(),
    "s+": date.getSeconds(),
  };
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + "";
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? str : padLeftZero(str)
      );
    }
  }
  return fmt;
  function padLeftZero(str) {
    return ("00" + str).substr(str.length);
  }
}
// 搜索信息接口
router.post("/searchpropertymsg", (req, res) => {
  // 接受前端传来的数据
  console.log(req.body);
  let { tit } = req.body;
  let sql = " select * from property where department like '%" + tit + "%' ";
  db.query(sql, tit, (results) => {
    results.map((item) => {
      item.time = formatDate(item.time, "yyyy-MM-dd");
      // console.log(item.time);
    });
    res.send({
      code: 200,
      magess: "搜索",
      results,
    });
  });
});

module.exports = router;
